//
//  XMSettingVC.m
//  XMWebTool
//
//  Created by zhangmingwei on 2023/12/11.
//

#import "XMSettingVC.h"
#import "XMCustomNaviView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "UIColor+XMTool.h"
#import "XMWebView.h"
#import "XMSizeMacro.h"
#import "UIView+XMFrame.h"
#import "XMWebViewManager.h"

#import "XMTimer.h"
#import "XMUserManager.h"

@interface XMSettingVC ()

@property (nonatomic, strong) XMCustomNaviView  *naviView;


@property (nonatomic, strong) UILabel   *timeLbl;
@property (nonatomic, strong) UISwitch  *timeSwitch;
@property (nonatomic, strong) UISlider  *timeSlider;

@end

@implementation XMSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getWhiteColor_1];
    self.fd_prefersNavigationBarHidden = YES;
    self.naviView = [XMCustomNaviView getInstanceWithTitle:@"设置"];
    [self.view addSubview:self.naviView];

    [self initAllView];
}

- (void)initAllView {
    [self.view addSubview:self.timeLbl];
    [self.view addSubview:self.timeSwitch];
    [self.view addSubview:self.timeSlider];
    
    self.timeLbl.frame = CGRectMake(10, kNaviStatusBarH_XM + 10, kScreenWidth_XM - 100, 40);
    self.timeSwitch.frame = CGRectMake(self.timeLbl.right, self.timeLbl.top, 80, 40);
    self.timeSlider.frame = CGRectMake(10, self.timeLbl.bottom + 10, kScreenWidth_XM - 20, 20);
    
    // 默认值：
    self.timeSlider.value = [XMUserManager sharedInstance].secondCount;
    [self.timeSwitch setOn:[XMUserManager sharedInstance].isOnFlag];
    [self reloadAction];
}

/// 刷新定时器相关展示
- (void)reloadTimeAction {
    self.timeSlider.value = [XMUserManager sharedInstance].secondCount;
    if (self.timeSwitch.isOn) {
        int minute = (int)(self.timeSlider.value) / 60;
        int second = (int)(self.timeSlider.value) % 60;
        self.timeLbl.text = [NSString stringWithFormat:@"%02d:%02d 后关闭音乐", minute, second];
    } else {
        self.timeLbl.text = [NSString stringWithFormat:@"未开启定时关闭功能"];
    }
}

- (void)reloadAction {
    if (self.timeSwitch.isOn) {
        [self reloadTimeAction];
        [[XMTimer defaultManager] xm_cancelTimerWithName:@"kStopMusic"];
        [[XMTimer defaultManager] xm_scheduleGCDTimerWithName:@"kStopMusic" interval:1 queue:dispatch_get_main_queue() repeats:YES action:^{
            NSLog(@"定时器执行中");
            if ([XMUserManager sharedInstance].secondCount > 0) {
                [XMUserManager sharedInstance].secondCount -= 1;
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifi_reloadPage" object:nil];
                [[XMTimer defaultManager] xm_cancelTimerWithName:@"kStopMusic"];
            }
            [self reloadTimeAction];
        }];
    } else {
        [self reloadTimeAction];
        [[XMTimer defaultManager] xm_cancelTimerWithName:@"kStopMusic"];
    }
}

// 点击的时候才执行
- (void)switchAction:(UISwitch *)mySwitch {
    NSLog(@"mySwitch====%d",mySwitch.isOn);
    [XMUserManager sharedInstance].isOnFlag = mySwitch.isOn;
    [self reloadAction];
}
// 手动滑动的时候才执行
- (void)sliderAction:(UISlider *)mySlider {
    NSLog(@"mySlider====%f",mySlider.value);
    [XMUserManager sharedInstance].secondCount = mySlider.value;
    [self reloadAction];
}

- (UILabel *)timeLbl {
    if (!_timeLbl) {
        _timeLbl = [[UILabel alloc] init];
        _timeLbl.textColor = [UIColor getBlackColor_1];
        _timeLbl.font = [UIFont systemFontOfSize:16];
    }
    return _timeLbl;
}

- (UISwitch *)timeSwitch {
    if (!_timeSwitch) {
        _timeSwitch = [[UISwitch alloc] init];
        [_timeSwitch setOn:[XMUserManager sharedInstance].isOnFlag];
        [_timeSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _timeSwitch;
}

- (UISlider *)timeSlider {
    if (!_timeSlider) {
        _timeSlider = [[UISlider alloc] init];
        _timeSlider.minimumValue = 0;
        _timeSlider.maximumValue = 150 * 60;
        _timeSlider.value = [XMUserManager sharedInstance].secondCount;
        [_timeSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _timeSlider;
}

@end
